package net.uhuijia.user.usermall.service.impl;
import net.uhuijia.enums.UserIdentity;
import net.uhuijia.shop.usershop.persistence.model.UserShop;
import net.uhuijia.shop.usershop.service.UserShopService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.uhuijia.appbase.service.CrudService;
import net.uhuijia.constants.DBConst;
import net.uhuijia.enums.UserLevel;
import net.uhuijia.user.usermall.persistence.dao.UserMallDao;
import net.uhuijia.user.usermall.persistence.model.UserMall;
import net.uhuijia.user.usermall.persistence.model.UserMallExample;
import net.uhuijia.user.usermall.service.UserMallService;
import net.uhuijia.user.userwxauth.persistence.model.UserWxauth;
import net.uhuijia.user.userwxauth.service.UserWxauthService;
import net.uhuijia.user.userwxopen.persistence.model.UserWxopen;
import net.uhuijia.user.userwxopen.service.UserWxopenService;
import net.uhuijia.utils.RandomStringHelper;
import net.uhuijia.utils.StringHelper;
import net.uhuijia.vo.user.usermall.param.LoginParamVo;

import java.util.List;


@Service
@Transactional
public class UserMallServiceImpl
     extends CrudService<UserMallDao,UserMall,UserMallExample> 
     implements UserMallService{
  
	@Autowired
	private UserWxopenService userWxopenService;
	@Autowired
	private UserWxauthService userWxauthService;
	@Autowired
	private UserShopService userShopService;
	
	@Override
	public UserMall reg(LoginParamVo param) {
		UserMall user = new UserMall();
		this.initDefalutFields(user);
		user.setPhone(param.getPhone());
		user.setNumber(getYhjRandomNumber());//用户编号
		user.setLevel(UserLevel.one.getState());//用户等级
		user.setUserPwd(RandomStringHelper.randomNumStr(6));//设置随机6位密码

		//看看能否获取到用户信息
		if( StringHelper.isNotBlanks(param.getUnionid(),param.getGhid()) ){
			 UserWxopen open = userWxopenService.getByUnionidAndGhid(param.getUnionid(),param.getGhid());
			 if( null != open ){
				 user.setNickName(open.getAuthNickName());
				 user.setArea(open.getAuthCountry());
				 user.setCity(open.getAuthCity());
				 user.setProvice(open.getAuthProvice());
				 user.setGender(open.getAuthSex());
				 user.setNickName(open.getAuthNickName());
				 user.setHeadImage(open.getAuthHeadImage());
				 open.setUserId(user.getId());
				 open.setNickName(user.getNickName());
				 userWxopenService.edit(open);
			 }
		}else if( StringHelper.isNotBlanks(param.getAppid(),param.getOpenid()) ){
			 UserWxauth auth = userWxauthService.getByAppidAndOpenid(param.getAppid(),param.getOpenid());
			 if( null != auth ) {
				 user.setNickName(auth.getAuthNickName());
				 user.setArea(auth.getAuthCountry());
				 user.setCity(auth.getAuthCity());
				 user.setProvice(auth.getAuthProvice());
				 user.setGender(auth.getAuthSex());
				 user.setNickName(auth.getAuthNickName());
				 user.setHeadImage(auth.getAuthHeadImage());
				 auth.setUserId(user.getId());
				 auth.setNickName(user.getNickName());
				 userWxauthService.edit(auth);
			 }
		}
		this.add(user);
		//发送短信通知用户密码
		this.sendPhoneMsg(user);
		return user;
	}
	
	/**
	 * 获得友惠家编号
	 * @return
	 * @author 许小宝
	 */
	public String getYhjRandomNumber(){
		//生成一个随机的8位数
		String randomNumStr = RandomStringHelper.yhjRandomNumStr(8);
		//查找是否存在
		UserMall byNumber = this.getByNumber(randomNumStr);
		if( null != byNumber ){
			return getYhjRandomNumber();
		}else{
			return randomNumStr;
		}
	}
	
	/**
	 * 根据用户编号查找用户
	 * @param number
	 * @return
	 * @author 许小宝
	 */
	public UserMall getByNumber( String number ){
		UserMall userMall = new UserMall();
		userMall.setIsEffective(DBConst.TRUE);
		userMall.setNumber(number);
		return mapper.getByNumber(userMall);
	}
	
	/**
	 * 发送短信通知用户密码及友惠家的编号
	 * @param user
	 * @return
	 * @author 许小宝
	 */
	public String sendPhoneMsg( UserMall user ){
		return SUCCESS;
	}

	/**
	 * 根据用户手机号码查找用户
	 * @author 许小宝
	 */
	@Override
	public UserMall getByPhone(String phone) {
		UserMall userMall = new UserMall();
		userMall.setIsEffective(DBConst.TRUE);
		userMall.setPhone(phone);
		return mapper.getByPhone(userMall);
	}

	/**
	 * 根据用户手机号码与密码查找用户
	 */
	@Override
	public UserMall getByPhonePwd(String phone, String pwd) {
		UserMall userMall = new UserMall();
		userMall.setIsEffective(DBConst.TRUE);
		userMall.setPhone(phone);
		userMall.setUserPwd(pwd);
		return mapper.getByPhonePwd(userMall);
	}

	@Override
	public UserMall getByEmailPwd(String email, String pwd) {
		UserMall userMall = new UserMall();
		userMall.setIsEffective(DBConst.TRUE);
		userMall.setEmail(email);
		userMall.setUserPwd(pwd);
		return mapper.getByEmailPwd(userMall);
	}

	@Override
	public UserMall getByNumberPwd(String number, String pwd) {
		UserMall userMall = new UserMall();
		userMall.setIsEffective(DBConst.TRUE);
		userMall.setNumber(number);
		userMall.setUserPwd(pwd);
		return mapper.getByNumberPwd(userMall);
	}

	/**
	 * 检测用户身份信息
	 * @param userMall 用户
	 * @param identity 需要效验的身份
	 * @return
	 */
	@Override
	public boolean checkUserMallIIdentity(UserMall userMall, UserIdentity identity) {
		if(userMall ==null || identity ==null)
			throw  new IllegalArgumentException();
		boolean flag = false;
		switch (identity){
			case Shop:
				List<UserShop> userShops =  this.userShopService.findUserShopByUserId(userMall.getId());
				if(!userShops.isEmpty())
					flag = true;
				break;
			default:
				break;
		}
		return flag;
	}
}